/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mlbs.session;

import com.mlbs.entity.Application;
import com.mlbs.entity.Device;
import com.mlbs.entity.Preferences;
import com.mlbs.entity.Usersession;
import com.mlbs.entity.Usertable;
import com.mlbs.helper.PrivacySettings;
import java.util.Collection;
import java.util.HashMap;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * This class provides the service of Privacy Control for the middleware. These include 
 * storage, retreival and clearing of privacy settings
 * @author hubharan
 */
@PersistenceContext(name="mlbs-ejbPU", unitName="mlbs-ejbPU")
@Stateless
public class PrivacyControlSBBean implements PrivacyControlSBLocal {
    @PersistenceContext(unitName="mlbs-ejbPU")
    private EntityManager em;    
/**
 * Get the privacy settings for a particular appId, deviceId based on the valid session
 * @param appId Application Identification
 * @param deviceId Device Identification
 * @param session Session object returned by authentication method
 * @return PrivacySettings object
 */
    public PrivacySettings getPrivacySettings(int appId, String deviceId, String session) {
        Application app = (Application)getEntityManager().
                createNamedQuery("Application.findByAppId").
                setParameter("appId", appId).getSingleResult();
        
        Device dev = (Device)getEntityManager().
                createNamedQuery("Device.findByDeviceid").
                setParameter("deviceid", deviceId).getSingleResult();
        Usertable user = dev.getUserid();
        Collection<Preferences> pref = user.getPreferencesCollection();
        //Get the pref for the device and app id from the collection
        Collection<Preferences> appPref = app.getPreferencesCollection();
        //compare the app pref and user pref and get correct values
        PrivacySettings settings = new PrivacySettings();
        HashMap userSettings = new HashMap();
        //set the values in Hashmap from pref
        settings.setUserSettings(userSettings);
        
        return settings;
    }
/**
 * Get all the privacy settings for a particular deviceId based on the valid session
 * @param deviceId
 * @param session
 * @return PrivacySettings object
 */
    public PrivacySettings getPrivacySettings(String deviceId, String session) {
        Device dev = (Device)getEntityManager().
                createNamedQuery("Device.findByDeviceid").
                setParameter("deviceid", deviceId).getSingleResult();
        Usertable user = dev.getUserid();
        Collection<Preferences> pref = user.getPreferencesCollection();
        //Get all the pref from the collection for a device
        PrivacySettings settings = new PrivacySettings();
        HashMap userSettings = new HashMap();
        //set the values in Hashmap from pref
        settings.setUserSettings(userSettings);
        
        return settings;
    }
/**
 * Get all the privacy settings of all the devices for a particular user based on the valid session
 * @param session Session object returned by the authentication method
 * @return PrivacySettings object
 */
    public PrivacySettings getPrivacySettings(String session) {
        Usersession sess = (Usersession)getEntityManager().
                createNamedQuery("Usersession.findBySession").
                setParameter("session", session).
                getSingleResult();
        Usertable user = (Usertable)getEntityManager().
                createNamedQuery("Usertable.findByUserId").
                setParameter("userId", sess.getId()).
                getSingleResult();
        Collection<Preferences> pref = user.getPreferencesCollection();
        //Get all the pref from the collection for the user
        PrivacySettings settings = new PrivacySettings();
        HashMap userSettings = new HashMap();
        //set the values in Hashmap from pref
        settings.setUserSettings(userSettings);
        
        return settings;

    }


/**
 * Set the privacy settings for a particular application and a particular 
 * device based on the privacy settings 
 * object passed
 * @param appId Applicatoin Identification
 * @param deviceId Device Identification
 * @param session Session object returned by the authenticaiton method
 * @param settingObj Settings object to set the privacy settings
 * @return true or false based on the resule of operation
 */
    public boolean setPrivacySettings(int appId, String deviceId, String session, PrivacySettings settingObj) {
        Application app = (Application)getEntityManager().
                createNamedQuery("Application.findByAppId").
                setParameter("appId", appId).getSingleResult();  
        Usersession sess = (Usersession)getEntityManager().
                createNamedQuery("Usersession.findBySession").
                setParameter("session", session).
                getSingleResult();
        Usertable user = (Usertable)getEntityManager().
                createNamedQuery("Usertable.findByUserId").
                setParameter("userId", sess.getId()).
                getSingleResult();        
        Preferences pref = new Preferences();
        pref.setAppId(app);
        pref.setUserId(user);
        HashMap hm = settingObj.getUserSettings();
        pref.setKeyName("");//get the key from Hashmap and set here
        pref.setValue("");//get the value from Hashmap and set here
        
        return true;
    }

    private EntityManager getEntityManager() {
        return em;
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
   
}
